package gdut.jmxywz.action;

import gdut.jmxywz.been.InfoFiles;
import gdut.jmxywz.been.MessageBoardFile;
import gdut.jmxywz.been.MessageFiles;
import gdut.jmxywz.been.StmessageBoardFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

@SuppressWarnings("serial")
public class FileDownloadAction extends BaseAction {

	private  String   filesId;
	private  String   fileUrl;
	private  InputStream inputStream;   
	private  String   contentType;
	public String getFileUrl() {
		return fileUrl;
	}
	public void setFileUrl(String fileUrl) {
		this.fileUrl = fileUrl;
	}
	
	public String getFilesId() {
		return filesId;
	}
	public void setFilesId(String filesId) {
		this.filesId = filesId;
	}
	public InputStream getInputStream() {
		return inputStream;
	}
	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
	public String getContentType() {
			for(String ext:contentTypes.keySet()){
				if(fileUrl.toLowerCase().endsWith(ext)){
					return contentTypes.get(ext);
				}
	     return "application/octet-stream";	
     }
		
		
		return contentType;
	}
	public void setContentType(String contentType) {
		
		this.contentType = contentType;
	}
	
	
	public static Map<String, String> getContentTypes() {
		return contentTypes;
	}


	static Map<String, String> contentTypes = new HashMap<String, String>();
	static {
		
		contentTypes.put("docx", "application/msword");
		contentTypes.put("xlsx", "application/vnd.ms-excel");
		contentTypes.put("pptx", "application/vnd.ms-powerpoint");
		
		contentTypes.put("doc", "application/msword");
		contentTypes.put("xls", "application/vnd.ms-excel");
		contentTypes.put("ppt", "application/vnd.ms-powerpoint");
		contentTypes.put("pdf", "application/pdf");
		contentTypes.put("exe", "application/octet-stream");
		contentTypes.put("bin", "application/octet-stream");
		contentTypes.put("zip", "application/zip");
		contentTypes.put("htm", "text/html");
		contentTypes.put("html", "text/html");
		contentTypes.put("gif", "image/gif");
		contentTypes.put("bmp", "image/bmp");
		contentTypes.put("jpeg", "image/jpeg");
		contentTypes.put("mpeg", "video/mpeg");
	}
      /***
       * 
       * 通知公告附件下载
       * @return
       * @throws Exception
       */
	@SuppressWarnings("deprecation")
	public String Infofiles() throws Exception{
		
		int id = 0;
		if(!"".equals(filesId)&&filesId!=null){
			
			id = Integer.parseInt(filesId);
		}
		System.out.println("infoFileIds---"+filesId);
		try {
			InfoFiles infoFiles =(InfoFiles) this.getCommonService().findByIntId(id, "gdut.jmxywz.been.InfoFiles");
			
			String path = ServletActionContext.getRequest().getRealPath("/");
			fileUrl=path+infoFiles.getFileUrl();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
		System.out.println("fileUrl---------"+fileUrl);
		  try {
		   	  File file = new File(fileUrl);
			  inputStream = new FileInputStream(file);
			  fileUrl = file.getName();
			  
			  System.out.println("----------"+fileUrl);
			  
			  contentType = getContentType();
			  
			  System.out.println("-----------"+contentType);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("------------系统找不到路径！-----------------------");
			e.printStackTrace();
			
		
		}
		  
		return  SUCCESS;
	}
	
	
    /***
     * 
     * 留言附件下载
     * @return
     * @throws Exception
     */
	public String TeacherMessageBoard() throws Exception{
		
		int id = 0;
		System.out.println(filesId);
		if(!"".equals(filesId)&&filesId!=null){
			
			id = Integer.parseInt(filesId);
		}
		System.out.println("filesId---"+id);
		try {
			
			MessageBoardFile mbf = (MessageBoardFile) this.getCommonService().findByIntId(id, "gdut.jmxywz.been.MessageBoardFile");
			
			String path = ServletActionContext.getRequest().getRealPath("/");
			
			fileUrl=path+mbf.getFileUrl();
			
			//下载次数加一
			mbf.setFileDownTime(mbf.getFileDownTime()+1);
			
			this.getCommonService().updateObject(mbf);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
		System.out.println("fileUrl---------"+fileUrl);
		  try {
		   	  File file = new File(fileUrl);
			  inputStream = new FileInputStream(file);
			  fileUrl = file.getName();
			  
			  System.out.println("----------"+fileUrl);
			  
			  contentType = getContentType();
			  
			  System.out.println("-----------"+contentType);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("------------系统找不到路径！-----------------------");
			e.printStackTrace();
			
		}
		  
		return SUCCESS;
	}
	
	/***
	 * 学生留言附件下载
	 * @return
	 * @throws Exception
	 */
	
    public String StudentMessageBoard() throws Exception{
		
		int id = 0;
		System.out.println(filesId);
		if(!"".equals(filesId)&&filesId!=null){
			
			id = Integer.parseInt(filesId);
		}
		System.out.println("filesId---"+id);
		try {
			
			StmessageBoardFile mbf = (StmessageBoardFile) this.getCommonService().findByIntId(id, "gdut.jmxywz.been.StmessageBoardFile");
			
			String path = ServletActionContext.getRequest().getRealPath("/");
			
			fileUrl=path+mbf.getFileUrl();
			
			//下载次数加一
			mbf.setFileDownTime(mbf.getFileDownTime()+1);
			
			this.getCommonService().updateObject(mbf);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
		System.out.println("fileUrl---------"+fileUrl);
		  try {
		   	  File file = new File(fileUrl);
			  inputStream = new FileInputStream(file);
			  fileUrl = file.getName();
			  
			  System.out.println("----------"+fileUrl);
			  
			  contentType = getContentType();
			  
			  System.out.println("-----------"+contentType);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("------------系统找不到路径！-----------------------");
			e.printStackTrace();
			
		}
		  
		return SUCCESS;
	}
   
     /***
      * 消息下载 
      * @return
      * @throws Exception
      */
     public String Message() throws Exception{
		
		int id = 0;
		System.out.println(filesId);
		if(!"".equals(filesId)&&filesId!=null){
			
			id = Integer.parseInt(filesId);
		}
		System.out.println("filesId---"+id);
		try {
			
			MessageFiles mbf = (MessageFiles) this.getCommonService().findByIntId(id, "gdut.jmxywz.been.MessageFiles");
			
			String path = ServletActionContext.getRequest().getRealPath("/");
			
			fileUrl=path+mbf.getFileUrl();
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
		System.out.println("fileUrl---------"+fileUrl);
		  try {
		   	  File file = new File(fileUrl);
			  inputStream = new FileInputStream(file);
			  fileUrl = file.getName();
			  
			  System.out.println("----------"+fileUrl);
			  
			  contentType = getContentType();
			  
			  System.out.println("-----------"+contentType);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("------------系统找不到路径！-----------------------");
			e.printStackTrace();
			
		}
		  
		return SUCCESS;
	}
}
